<?php 
	require_once '../function.php';

	$user = xiu_get_current_user_admin();
	// 当前url
	$current_url = $_SERVER['PHP_SELF'].'?';
	// if (!empty($_SERVER["QUERY_STRING"])) {
	// 	$current_url .= '?'.$_SERVER["QUERY_STRING"].'&';
	// }

	// 获取所有分类数据
	// =================================================================
	$categories = xiu_fetch_db_all('select * from categories;');

	$where = "posts.user_id = {$user['id']}";
	// 分类
	if (isset($_GET['c']) && $_GET['c'] != -1) {
		$where .= 'and posts.category_id='.$_GET['c'];
		//获取当前的url
		$current_url .= 'c='.$_GET['c'].'&';
	}
	// 状态
	if (isset($_GET['s']) && $_GET['s'] != -1) {
		$where .= ' and posts.status='.$_GET['s'];
		$current_url .= 's='.$_GET['s'].'&';
	}

	// 分页
	// =================================================================================
	// 每页10条数据
	$size = 10;
	// 最大页数
	$max_page = (int)ceil(xiu_fetch_db_one("select count(1) as num from posts inner join categories on posts.category_id = categories.id inner join users on posts.user_id = users.id where {$where};")['num'] / $size) ;


	// 如果没有数据,则页码显示为1
	$max_page = $max_page === 0 ? 1 : $max_page;

	// 获取当前传递的页码,实现分页
	$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
	// 如果输入页码大于最大,则取最大
	$page = $page > $max_page ? $max_page : $page;
	// 如果输入页码小于1,则取1
	$page = $page <= 0 ? 1 : $page;
	$offset = ($page - 1) * $size;
	//分页可见页码
	$visible = 5;
	//页码区间
	$region = ($visible - 1) / 2;
	// 开始页码
	$begin = $page - $region;
	// 结束页码
	$end = $page + $region;
	if ($begin <= 0) {
		$begin = 1;
		$end = $begin + 4;
	}
	if ($end > $max_page) {
		$end  = $max_page;
		$begin = $end - 4;

		// 当总页数小于5页时
		if ($begin <= 0) {
			$begin = 1;
		}
	}


	// 获取当前所有文章数据
	// ==================================================================
	$posts = xiu_fetch_db_all("select 
		posts.id,
		posts.title,
		posts.created,
		posts.status,
		categories.name as category_name,
		users.nickname 
		from posts 
		inner join categories on posts.category_id = categories.id 
		inner join users on posts.user_id = users.id 
		where {$where}
		order by posts.created desc 
		limit {$offset}, {$size};");

	// 如果没有数据,则将$posts设为一个空数组
	$posts = $posts === null ? [] : $posts;


	// 将状态数字转换成中文输出
	// =================================================================
	function convers_status($status){
		if ($status === '0') {
			return '回收站';
		}else if($status === '1'){
			return '草稿箱';
		}else{
			return '已发布';
		}
	}

	// 转换时间
	// =================================================================
	function convers_time($time){
		$time = strtotime($time);
		return date('Y年m月d日<b\r>H:i:s', $time);
	}

 ?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>所有文章</title>
		<link rel="stylesheet" type="text/css" href="/static/vendors/bootstrap4.6/css/bootstrap.css">
		<link rel="stylesheet" type="text/css" href="/static/css/admin.css">
	</head>
	<body>
		<div class="main">
			<?php include './inc/navbar.php' ?>
			<div class="container-fluid">
			<div class="page-title">
				<h1>所有文章</h1>
				<a class="btn btn-primary btn-sm" href="/admin/post-ad.php">写文章</a>
			</div>
			<div class="page-container">
				<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="get" class="form-inline pull-left">
					<select class="form-control input-sm" name="c">
						<option value="-1">所有分类</option>
						<?php foreach ($categories as $item): ?>
							<option value="<?php echo $item['id']; ?>" <?php echo isset($_GET['c'])&&$item['id'] == $_GET['c']? 'selected' : ''; ?>><?php echo $item['name'] ?></option>
						<?php endforeach ?>
					</select>
					<select class="form-control input-sm" name="s">
						<option value='-1' <?php echo isset($_GET['s'])&&$_GET['s'] == -1? 'selected':'' ?>>所有状态</option>
						<option value="0" <?php echo isset($_GET['s'])&&$_GET['s'] == 0? 'selected':'' ?>>回收站</option>
						<option value='1' <?php echo isset($_GET['s'])&&$_GET['s'] == 1? 'selected':'' ?>>草稿</option>
						<option value='2' <?php echo isset($_GET['s'])&&$_GET['s'] == 2? 'selected':'' ?>>已发布</option>
					</select>
					<button class="btn btn-defaul">筛选</button>
				</form>
				<a id="posts_delete" class="btn btn-danger btn-sm" style="display: none;">批量删除</a>
				<ul class="pagination">
					<!-- 当前页码大于1显示上一页 -->
					<?php if ($page > 1): ?>
						<li><a href="<?php echo $current_url.'page='.($page - 1); ?>" class="text-center">上一页</a></li>
					<?php endif ?>
					<?php for ($i=$begin; $i <= $end; $i++):?>
						<li><a href="<?php echo $current_url.'page='.$i ?>" class="<?php echo $page === $i ? 'active_page' : ''; ?>"><?php echo $i ?></a></li>
					<?php endfor; ?>
					<!-- 当前页码小于最大页面 -->
					<?php if ($page < $max_page): ?>
						<li><a href="<?php echo $current_url.'page='.($page + 1); ?>">下一页</a></li>
					<?php endif ?>
				</ul>
			</div>	
			<table class="table table-bordered table-hover">
				<thead>
					<tr>
						<th width="40px"><input type="checkbox"></th>
						<th>ID</th>
						<th>标题</th>
						<th>作者</th>
						<th>分类</th>
						<th>发表时间</th>
						<th>状态</th>
						<th width="150px">操作</th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ($posts as $key => $item): ?>
						<tr>
							<th width="40px"><input type="checkbox" data-id="<?php echo $item['id'] ?>"></th>
							<th><?php echo $key+1; ?></th>
							<th><?php echo $item['title'] ?></th>
							<th><?php echo $item['nickname'] ?></th>
							<th><?php echo $item['category_name'] ?></th>
							<th><?php echo convers_time($item['created']); ?></th>
							<th><?php echo convers_status($item['status']); ?></th>
							<th>
								<a href="/admin/post-ad.php?id=<?php echo $item['id']; ?>" class="btn btn-primary btn-sm">编辑</a>
								<a href="/admin/posts-delete.php?id=<?php echo $item['id'] ?>" class="btn btn-danger btn-sm">删除</a>
							</th>
						</tr>
					<?php endforeach ?>
					
				</tbody>
				
			</table>
		</div>
		</div>
		<?php $current_page = 'posts' ?>
		<?php include './inc/asid.php' ?>
		<script src="/static/vendors/jQuery.js"></script>
		<script src="/static/vendors/bootstrap4.6/js/bootstrap.js"></script>
		<script type="text/javascript">
			$(function(){
				var checkboxs = $('tbody input');
				var posts_delete = $('#posts_delete');
				// 创建一个数组,将所有选中的分类的id都添加进去;
				var checkedId = [];
				checkboxs.on('change', function(){
					var id = $(this).data('id');
					// 判断当前是否选中,如果选中则添加进数组
					if ($(this).prop('checked')) {
						checkedId.push(id);
					}else{
						checkedId.pop(id);
					}
					checkedId.length? posts_delete.fadeIn() : posts_delete.fadeOut();
					// 设置批量删除的href属性
					posts_delete.attr('href', '/admin/posts-delete.php?id='+checkedId);
					console.log(checkedId);
				})

				// 全选和全不选
				$('thead input').on('change', function(){
					var checked = $(this).prop('checked');
					checkboxs.prop('checked', checked).trigger('change');
				})
			})
		</script>
	</body>
</html>